package controllers;

import models.Assignment;
import models.Submission;
import models.TestRun;
import others.DumbFS;
import play.mvc.Controller;
import play.mvc.With;

@With (Secure.class)
public class Download extends Controller {
	public static void submission(long id) {
		Submission sub = Submission.findById(id);
		if (sub == null || sub.student != Security.getStudent() && !(Security.getConnected() instanceof Instructor)) {
			notFound();
		}
		response.setHeader("Content-Disposition", "attachment; filename=" + sub.getFile().getName());
		renderBinary(DumbFS.getInstance().getSubmission(sub));
	}

	@Check ("ta")
	public static void testFile(long id) {
		TestRun run = TestRun.findById(id);
		notFoundIfNull(run);
		renderBinary(run.test.get(), run.test.filename);
	}

	@Check ("ta")
	public static void CSVReport(long id) {
		String report = "";
		Assignment ass = Assignment.findById(id);
		for (Submission sub : ass.submissions) {
			report += sub.student.username + "," + sub.grade + "\n";
		}
		response.setHeader("Content-Disposition", "attachment; filename=" + ass.id + "- " + ass.title + ".csv");
		renderText(report);
	}
}
